/*
 * Funny Language - a free style programming language.
 * Copyright (C) 2015 by fanguangping (fanguangping@163.com)
 * test_mapqueue.c
 */

#include <string.h>
#include "runner_ds.h"

static void test_alloc_map_node() {
	KeyValueNode *node = alloc_map_node();
	assert_non_null(node);
}

static void test_add_key_value() {
	add_key_value(NULL, NULL, NULL);
}

static void test_find_value() {
	String key1 = "a";
	String key2 = "b";
	String value1 = "123";
	String value2 = "456";

	Queue *queue = init_queue();
	add_key_value(queue, key1, value1);
	add_key_value(queue, key2, value2);

	assert_ptr_equal(find_value(queue, key1), value1);
	assert_ptr_equal(find_value(queue, key2), value2);
	assert_null(find_value(queue, NULL));
	assert_null(find_value(NULL, key1));
	assert_null(find_value(NULL, NULL));
}

static void test_find_values() {
	String key1 = "a";
	String key2 = "b";
	String value1 = "123";
	String value2 = "456";
	String value3 = "321";
	String value4 = "654";

	Queue *queue = init_queue();
	add_key_value(queue, key1, value1);
	add_key_value(queue, key2, value2);
	add_key_value(queue, key1, value3);
	add_key_value(queue, key2, value4);

	Queue *values_for_key1 = find_values(queue, key1);
	Queue *values_for_key2 = find_values(queue, key2);

	assert_ptr_equal(values_for_key1->head->element, value1);
	assert_ptr_equal(values_for_key1->head->next->element, value3);
	assert_ptr_equal(values_for_key2->head->element, value2);
	assert_ptr_equal(values_for_key2->head->next->element, value4);
	assert_null(find_values(queue, NULL));
	assert_null(find_values(NULL, key1));
	assert_null(find_values(NULL, NULL));
}

int main(int argc, char **argv) {
	const struct CMUnitTest tests[] = {
			cmocka_unit_test(test_alloc_map_node),
			cmocka_unit_test(test_add_key_value),
			cmocka_unit_test(test_find_value),
			cmocka_unit_test(test_find_values),
	};
	return cmocka_run_group_tests(tests, NULL, NULL);
}
